package com.example.startapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.startapp.R;
import com.example.startapp.util.DBOpenHelper;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 这是注册页面
 */

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TYPE_REGISTER = "注册";
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.text_bar)
    TextView text_bar;
    @BindView(R.id.lin_register)
    LinearLayout linearLayout;//隐藏起来
    @BindView(R.id.ed_pd)
    EditText ed_pd;//密码
    @BindView(R.id.ed_name)
    EditText ed_name;//用户名
    DBOpenHelper mDBOpenHelper;
    @BindView(R.id.btn_login) TextView btn_register;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        ButterKnife.bind(this);
        mDBOpenHelper = new DBOpenHelper(this);
        init();
    }


    private void init() {
        back.setOnClickListener(this);
        btn_register.setOnClickListener(this);
        text_bar.setText("注册");
        btn_register.setText("注册");

        linearLayout.setVisibility(View.GONE);
        ed_pd.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);

        ed_pd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            /*
            返回 会员中心
             */
            case R.id.back:
               Intent intent_back=new Intent(this,MainActivity.class);
                intent_back.putExtra("frag", 4);
                startActivity(intent_back);
                finish();


                break;
            case R.id.btn_login:
              /*
              验证注册
               */
                String name = ed_name.getText().toString();
                String password = ed_pd.getText().toString();
                //用户名和密码不为空
                if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password)) {
                    //存储到数据库;
                    mDBOpenHelper.add(name, password, TYPE_REGISTER);
                    Intent intent_register = new Intent(this, MainActivity.class);
                    intent_register.putExtra("frag", 1);
                    startActivity(intent_register);
                    finish();
                    Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();


                } else {
                    Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show();

                }

                break;
        }
    }
}
